package com.come4loves.game.module.card.helper;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.come4loves.core.context.SpringContext;
import com.come4loves.core.utils.Splitable;
import com.come4loves.game.dataconfig.model.CardConfig;
import com.come4loves.game.dataconfig.model.EquipCardConfig;
import com.come4loves.game.dataconfig.model.GeneralCardConfig;
import com.come4loves.game.dataconfig.model.SpellCardConfig;
import com.come4loves.game.dataconfig.model.UnitCardConfig;
import com.come4loves.game.dataconfig.service.EquipCardService;
import com.come4loves.game.dataconfig.service.GeneralCardService;
import com.come4loves.game.dataconfig.service.SpellCardService;
import com.come4loves.game.dataconfig.service.UnitCardService;
import com.come4loves.game.dbproxy.entity.Card;
import com.come4loves.game.module.card.constant.CardType;
import com.come4loves.game.module.card.dao.CardDao;
import com.come4loves.game.module.card.dao.CardGroupDao;
import com.come4loves.game.module.card.model.CardGroupModel;
import com.come4loves.game.module.card.model.CardModel;
import com.come4loves.game.module.fight.constants.SkillMoment;
import com.come4loves.game.module.fight.model.FightCard;
import com.come4loves.game.module.skill.model.SkillModel;

public class CardHelper {

	protected static Logger LOGGER = LoggerFactory.getLogger(CardHelper.class);
	/**
	 * 获取武将牌列表
	 * 
	 * @param actorId
	 * @return
	 */
	public static List<FightCard> getGeneralFightCard(long actorId) {
		CardDao dao = (CardDao) SpringContext.getBean(CardDao.class);
		Card cards = dao.getCard(actorId);
		Map<Integer, CardModel> generCardsMap = cards.generCardsMap;
		List<FightCard> cardList = new ArrayList<FightCard>();
		for (Map.Entry<Integer, CardModel> entry : generCardsMap.entrySet()) {
			FightCard fightCard = FightCard.valueOf(CardType.GENERAL, entry.getValue());
			cardList.add(fightCard);
		}
		return cardList;
	}

	/**
	 * 获取手牌列表
	 * 
	 * @param actorId
	 * @return
	 */
	public static List<FightCard> generateFightCard(long actorId) {
		CardDao dao = (CardDao) SpringContext.getBean(CardDao.class);
		Card cards = dao.getCard(actorId);
		List<FightCard> cardList = new ArrayList<FightCard>();
		for (Map.Entry<Integer, CardModel> entry : cards.equipCardsMap.entrySet()) {
			FightCard fightCard = FightCard.valueOf(CardType.EQUIP, entry.getValue());
			cardList.add(fightCard);
		}
		for (Map.Entry<Integer, CardModel> entry : cards.spellCardsMap.entrySet()) {
			FightCard fightCard = FightCard.valueOf(CardType.SPELL, entry.getValue());
			cardList.add(fightCard);
		}
		for (Map.Entry<Integer, CardModel> entry : cards.unitCardsMap.entrySet()) {
			FightCard fightCard = FightCard.valueOf(CardType.UNIT, entry.getValue());
			cardList.add(fightCard);
		}
		return cardList;
	}

	/**
	 * 根据acotrId和牌组id获取fightcard组
	 * 
	 * @param actorId
	 * @param groupId
	 * @return
	 */
	public static List<FightCard> getFightCardFromCardGroup(long actorId, int groupId) {
		List<FightCard> cardList = new ArrayList<FightCard>();
		CardGroupDao dao = (CardGroupDao) SpringContext.getBean(CardGroupDao.class);
		CardGroupModel cardGroupModel = dao.getCardGroupModel(actorId, groupId);
		for (CardModel model : cardGroupModel.cardList) {
			cardList.add(FightCard.valueOf(model.type, model));
		}
		return cardList;
	}

	public static FightCard getDataConfigByCardType(FightCard fightCard, CardType type, int cfgId) {
		switch (type) {
		case GENERAL:
			GeneralCardConfig generalCardConfig = GeneralCardService.get(cfgId);
			fightCard.setCfgId(generalCardConfig.getId());
			fightCard.setAttack(generalCardConfig.getAttack());
			fightCard.setHp(generalCardConfig.getHitpoint());
			fightCard.setMaxHp(fightCard.getHp());
			fightCard.setMagic(generalCardConfig.getCost());
			fightCard.setSpeed(generalCardConfig.getSpeed());
			return fightCard;
		case UNIT: {
			UnitCardConfig unitCardConfig = UnitCardService.get(cfgId);
			fightCard.setCfgId(unitCardConfig.getId());
			fightCard.setAttack(unitCardConfig.getAttack());
			fightCard.setHp(unitCardConfig.getHitpoint());
			fightCard.setMaxHp(fightCard.getHp());
			fightCard.setMagic(unitCardConfig.getCost());
			fightCard.setSpeed(unitCardConfig.getSpeed());
			parseSkill(fightCard, unitCardConfig);
			

			return fightCard;
		}
		case EQUIP:
			EquipCardConfig config = EquipCardService.get(cfgId);
			fightCard.setCfgId(config.getId());
			fightCard.setAttack(config.getAttack());
			fightCard.setHp(config.getHitpoint());
			fightCard.setMaxHp(fightCard.getHp());
			fightCard.setMagic(config.getCost());
			fightCard.setSpeed(config.getSpeed());
			parseSkill(fightCard, config);
			return fightCard;
		case SPELL: {
			SpellCardConfig spellCardConfig = SpellCardService.get(cfgId);
			fightCard.setCfgId(spellCardConfig.getId());
			fightCard.setAttack(0);
			fightCard.setHp(0);
			fightCard.setMaxHp(fightCard.getHp());
			fightCard.setMagic(spellCardConfig.getCost());
			fightCard.setSpeed(spellCardConfig.getSpeed());
			parseSkill(fightCard, spellCardConfig);
			
			return fightCard;

		}

		default:
			break;
		}
		return null;
	}
	
	private static void parseSkillStr(FightCard fightCard,String[] skillEvents, String[] skillConductinNums,String[] skillActionNums, String[] casttargets,String[] aitargets){
		if (skillEvents.length != skillConductinNums.length || skillEvents.length != skillActionNums.length){
			LOGGER.error("skill config error,id:{},type:{}", fightCard.getCfgId(), fightCard.getCardType());
			return;
		}
		for (int i = 0; i < skillEvents.length; i++) {
			parseSkill(fightCard, skillEvents[i],skillConductinNums[i],skillActionNums[i], casttargets[0], aitargets[0]);
		}
	}
	private static void parseSkill(FightCard fightCard, String skillEvent,String skillConductionNum,String skillActionNum, String casttarget, String aitarget){
		String tmp = skillEvent;
		SkillMoment skillMoment = SkillMoment.vallueOf(tmp);
		if (skillMoment == null) {
			LOGGER.error("skill moment error, skillEvent:{}, cardInfo:{}",tmp,fightCard.simpleInfo());
			return;
		}
		SkillModel skillModel = new SkillModel();
		String tmp1 = skillConductionNum;
		String[] tmp1s = tmp1.split(Splitable.ELEMENT_SPLIT);
		for (String string : tmp1s) {
			String[] values = string.split(Splitable.BETWEEN_UNDERLINE);
			if (values.length == 2) {
				if (values[0].startsWith("target")) {
					skillModel.getTargets().put(values[0], Integer.valueOf(values[1]));
				} else {
					skillModel.getCondition().put(values[0], Integer.valueOf(values[1]));
				}
			}
		}
		String tmp2 = skillActionNum;
		String[] tmp2s = tmp2.split(Splitable.ELEMENT_SPLIT);
		for (String string : tmp2s) {
			String[] values = string.split(Splitable.BETWEEN_UNDERLINE);
			if (values.length == 2) {
				skillModel.getEffect().put(values[0], values[1]);
			}
		}
		String tmp3 = casttarget;
		String[] tmp3s = tmp3.split(Splitable.ELEMENT_SPLIT);
		for (String string : tmp3s) {
			String[] values = string.split(Splitable.BETWEEN_UNDERLINE);
			if (values.length == 2) {
				fightCard.getCastTargets().put(values[0], Integer.valueOf(values[1]));
			}
		}
		String tmp4 = aitarget;
		String[] tmp4s = tmp4.split(Splitable.ELEMENT_SPLIT);
		for (String string : tmp4s) {
			String[] values = string.split(Splitable.BETWEEN_UNDERLINE);
			if (values.length == 2) {
				fightCard.getAiTargets().put(values[0], Integer.valueOf(values[1]));
			}
		}
		
		fightCard.addSkill(skillMoment, skillModel);
	}
	
	private static void parseSkill(FightCard fightCard,CardConfig cardConfig){
		String[] skillEvents = cardConfig.getSkillevent().split(Splitable.DELIMITER_ARGS);
		String[] skillConductinNums = cardConfig.getSkillconductionnum().split(Splitable.DELIMITER_ARGS);
		String[] skillActionNums = cardConfig.getSkillactionnum().split(Splitable.DELIMITER_ARGS);
		String[] casttargets = cardConfig.getCasttarget().split(Splitable.DELIMITER_ARGS);
		String[] aitargets = cardConfig.getAItarget().split(Splitable.DELIMITER_ARGS);
		parseSkillStr(fightCard, skillEvents, skillConductinNums,skillActionNums,casttargets,aitargets);
		
	}
}
